<template>
  <ContentWrap v-loading="formLoading">
    <el-tabs v-model="activeName">
      <el-tab-pane label="基础设置" name="info">
        <InfoForm
          ref="infoRef"
          v-model:activeName="activeName"
          :is-detail="isDetail"
          :propFormData="formData"
        />
      </el-tab-pane>
      <el-tab-pane label="房产维度" name="sku">
        <SkuForm
          ref="skuRef"
          v-model:activeName="activeName"
          :is-detail="isDetail"
          :propFormData="formData"
        />
      </el-tab-pane>
      <el-tab-pane label="房产详情" name="description">
        <DescriptionForm
          ref="descriptionRef"
          v-model:activeName="activeName"
          :is-detail="isDetail"
          :propFormData="formData"
        />
      </el-tab-pane>
    </el-tabs>
    <el-form>
      <el-form-item style="float: right">
        <el-button v-if="!isDetail" :loading="formLoading" type="primary" @click="submitForm">
          保存
        </el-button>
        <el-button @click="close">返回</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>
</template>
<script lang="ts" setup>
import { cloneDeep } from 'lodash-es'
import { useTagsViewStore } from '@/store/modules/tagsView'
import * as HouseSpuApi from '@/api/house/house/spu'
import InfoForm from './InfoForm.vue'
import DescriptionForm from './DescriptionForm.vue'
import SkuForm from './SkuForm.vue'

defineOptions({ name: 'HouseSpuAdd' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗
const { push, currentRoute } = useRouter() // 路由
const { params, name } = useRoute() // 查询参数
const { delView } = useTagsViewStore() // 视图操作

const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const activeName = ref('info') // Tag 激活的窗口
const isDetail = ref(false) // 是否查看详情
const infoRef = ref() // 商品信息 Ref
const skuRef = ref() // 商品规格 Ref
const descriptionRef = ref() // 商品详情 Ref
// SPU 表单数据
const formData = ref<HouseSpuApi.Spu>({
  name: '', // 商品名称
  oldArea: 0, //旧面积
  area: 0, //新面积
  categoryId: undefined, // 商品分类
  specType: false, // 商品规格
  skus: [
    {
      name: "", // 名称
      oldArea: 0, // 旧面积
      area: 0, // 新面积
      status: 1, // 状态
      useNewArea: false, // 是否使用新面积
      isCalculate: 0
    }
  ],
  description: '', // 详情
})

/** 获得详情 */
const getDetail = async () => {
  if ('HouseSpuDetail' === name) {
    isDetail.value = true
  }
  const id = params.id as unknown as number
  if (id) {
    formLoading.value = true
    try {
      const res = (await HouseSpuApi.getSpu(id)) as HouseSpuApi.Spu
      formData.value = res
    } finally {
      formLoading.value = false
    }
  }
}

/** 提交按钮 */
const submitForm = async () => {
  // 提交请求
  formLoading.value = true
  try {
    // 校验各表单
    await unref(infoRef)?.validate()
    await unref(skuRef)?.validate()
    await unref(descriptionRef)?.validate()
    // 深拷贝一份, 这样最终 server 端不满足，不需要影响原始数据
    const deepCopyFormData = cloneDeep(unref(formData.value)) as HouseSpuApi.Spu
    // 校验都通过后提交表单
    const data = deepCopyFormData as HouseSpuApi.Spu
    const id = params.id as unknown as number
    if (!id) {
      await HouseSpuApi.createSpu(data)
      message.success(t('common.createSuccess'))
    } else {
      await HouseSpuApi.updateSpu(data)
      message.success(t('common.updateSuccess'))
    }
    close()
  } finally {
    formLoading.value = false
  }
}

/** 关闭按钮 */
const close = () => {
  delView(unref(currentRoute))
  push({ name: 'HouseSpu' })
}

/** 初始化 */
onMounted(async () => {
  await getDetail()
})
</script>
